package com.stop.controller;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.stop.util.StopConstants;

public class StopController {
	
	private static StopController instancia;

	protected StopController() {
		
	}

	public JSONObject salas() throws Exception{
		// Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost(new URI(StopConstants.LOOKUP_SERVER + "ListarSalas"));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost, SessionController.localContext());
        
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        
        response.getEntity().writeTo(stream);
        
        JSONObject retorno = new JSONObject(stream.toString("UTF-8"));
        
        return retorno;
	}
	
	public JSONObject entrarSala(String uid, String option) throws Exception{
		// Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost(new URI(StopConstants.LOOKUP_SERVER + "EntrarSala"));

	    // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("uid", uid));
        nameValuePairs.add(new BasicNameValuePair("option", option));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	    
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost, SessionController.localContext());
        
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        
        response.getEntity().writeTo(stream);
        
        JSONObject retorno = new JSONObject(stream.toString("UTF-8"));
        
        return retorno;
	}
	
	public JSONObject sairSala(String uid) throws Exception{
		// Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost(new URI(StopConstants.LOOKUP_SERVER + "SairSala"));

	    // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("uid", uid));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	    
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost, SessionController.localContext());
        
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        
        response.getEntity().writeTo(stream);
        
        JSONObject retorno = new JSONObject(stream.toString("UTF-8"));
        
        return retorno;
	}
	
	public JSONObject criarSala(JSONObject sala) throws Exception{
		// Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost(new URI(StopConstants.LOOKUP_SERVER + "CriarSala"));

	    // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("sala", sala.toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	    
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost, SessionController.localContext());
        
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        
        response.getEntity().writeTo(stream);
        
        JSONObject retorno = new JSONObject(stream.toString("UTF-8"));
        
        return retorno;
	}
	
	public JSONObject login() throws Exception{
		// Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost(new URI(StopConstants.LOOKUP_SERVER + "EntrarJogo"));

	    // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("nome", "Weslei Ragnini"));
        nameValuePairs.add(new BasicNameValuePair("email", "wragnini@gmail.com"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	    
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost, SessionController.localContext());
        
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        
        response.getEntity().writeTo(stream);
        
        JSONObject retorno = new JSONObject(stream.toString("UTF-8"));
        
        return retorno;
	}
	
	public static StopController getInstancia() {
		if(instancia==null){
			//instancia = new StopController();
			instancia = new StopControllerFake();
		}
		return instancia;
	}

}
